package com.beyondsoft.sabg.edu.config;


import com.aliyun.aimiaobi20230801.Client;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;

/**
 * 妙笔启动检查器
 */
@Component
@Slf4j
public class AiMiaoBiStartupChecker implements CommandLineRunner {

    @Autowired
    private Client aiMiaoBiClient;

    @Value("${aliyun.aimiaobi.endpoint}")
    private String endpoint;

    @Value("${aliyun.aimiaobi.workspace-id}")
    private String workspaceId;

    @Value("${aliyun.aimiaobi.writing-scene}")
    private String writingScene;


    @Value("${aliyun.aimiaobi.language}")
    private String language;

    @Value("${aliyun.aimiaobi.gc-number-size}")
    private Integer gcNumberSize;

    @Value("${aliyun.aimiaobi.gc-number-size-tag}")
    private String gcNumberSizeTag;

    @Override
    public void run(String... args) throws Exception {
        log.info("=== 阿里云妙笔配置检查 ===");

        try {

            // 检查客户端
            if (aiMiaoBiClient != null) {
                log.info("✓ 妙笔客户端初始化成功");
            } else {
                log.error("✗ 妙笔客户端初始化失败");
            }

            // 检查凭据（通过环境变量或配置文件）
            String accessKeyId = System.getenv("ALIBABA_CLOUD_ACCESS_KEY_ID");
            String accessKeySecret = System.getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET");

            if (accessKeyId != null && accessKeySecret != null) {
                log.info("✓ 检测到环境变量凭据配置");
            } else {
                log.warn("⚠ 未检测到环境变量凭据，请确保已配置阿里云凭据");
                log.info("凭据配置方式：");
                log.info("1. 环境变量: ALIBABA_CLOUD_ACCESS_KEY_ID 和 ALIBABA_CLOUD_ACCESS_KEY_SECRET");
                log.info("2. 配置文件: ~/.alibabacloud/credentials");
                log.info("3. 实例角色: 在ECS实例上自动获取");
            }

            log.info("=== 妙笔配置检查完成 ===");

        } catch (Exception e) {
            log.error("妙笔配置检查失败", e);
            log.error("请检查以下配置项：");
            log.error("1. 阿里云凭据是否正确配置");
            log.error("2. 工作空间ID是否有效");
            log.error("3. 网络连接是否正常");
        }
    }
}